<?php

class IndexController extends CF_Controller_Action
{

	function init(){
		if ($this->getRequest()->isXmlHttpRequest()) {
			//if AJAX - swith of templates rendering
			$ajaxContext = $this->_helper->getHelper('AjaxContext');
			$ajaxContext
			->addActionContext('index', array('json','html'))
			->initContext();
		}
	}

    /**
     *
     * 
     */
    public function indexAction()
    {
    	$request = $this->getRequest();
    	if($request->isXmlHttpRequest()){
    		$linkID = $this->_getParam('link_id');
       		//array of array(k1=general_number,k2=tag)
    		$responseOutput['clouds'] = Application_Model_RssReaderApi::getCloudTags($linkID, true);
    		$responseOutput['link_id'] = $linkID;
    		$this->_helper->json($responseOutput, true);
    	}else{
    		$links = Application_Model_RssReaderApi::getLinks(); 	
    		$this->view->links = $links;
    		$this->view->cloudTags = Application_Model_RssReaderApi::getCloudTags();
    	}
    	//
    	//echo '<pre>';
    	//var_dump($occur);
    	//exit;

    }
    /**
     *
     * Enter description here ...
     */
	public function deleteAction(){
		$id = $this->_getParam('link_id');
		if(empty($id)){
			$this->_helper->redirector('index');
		}else{
			Application_Model_RssReaderApi::deleteLink($id);
			$this->_helper->redirector('index');
		}
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function addAction(){
		$request = $this->getRequest();
		$form = new Application_Form_Link();

		$this->view->title = 'Edit Source';
		$this->view->headTitle($this->view->title);
		$this->view->form = $form;
		$id = $this->_getParam('link_id',0);
		if($request->isPost()){
			$formData = $request->getPost();
			if($form->isValid($formData)){
				$linkAttributes = array();
				$linkAttributes['url'] = $form->getValue('url');
				$linkAttributes['title'] = $form->getValue('title');
				$linkAttributes['category_id'] = $form->getValue('category_id');

				
				Application_Model_RssReaderApi::addLink($linkAttributes);
				$this->_helper->redirector('index');
			}else{
				$form->setDefaults($formData);
			}
		}else{
			$form->populate($request->getPost());
		}
	}
	/**
	 *
	 * Enter description here ...
	 */
	public function editAction(){
		$request = $this->getRequest();
		$form = new Application_Form_Link();

		error_reporting(E_ALL);

		$this->view->title = 'Edit Source';
		$this->view->headTitle($this->view->title);
		$this->view->form = $form;
		$id = $this->_getParam('link_id',0);
		if($request->isPost()){

			$formData = $request->getPost();

			if($form->isValid($formData)){
				$linkAttributes = array();
				$linkAttributes['url'] = $form->getValue('url');
				$linkAttributes['title'] = $form->getValue('title');
				$linkAttributes['category_id'] = $form->getValue('category_id');
				//$linkAttributes['id'] = $id;
				Application_Model_RssReaderApi::updateLink($linkAttributes, $id);
				$this->_helper->redirector('index');
			}else{
				$form->setDefaults($formData);
			}
		}else{
			if($id > 0){

				$link = Application_Model_RssReaderApi::getLinks($id);
				$form->setDefaults(array(
					'category_id' => $link->category_id,
					'title' => $link->title,
					'url' => $link->url
				));
			}
		}
	}

}













